/******************************************/
/******************************************/
/****  JS Document                     ****/
/****  type/code 2010                  ****/
/****  by Andrew Mahon, Lev Kanter     ****/
/******************************************/
/******************************************/

(function(tc) {
	tc.jQ.fn.blogPost = function(options){
		var $tbp = this, _o = {}, _commentsOpen = false, _postOpen = false, _moreContentLoaded = false
		
		_o = tc.jQ.extend({
			id:null,
			hideCommentsOnLoad:false,
			hidePostOnLoad:false
		},options)
		
		function _initialize(){
			tc.util.log("type/code Blog Post Starting",'info')
			_setup_events()
			if(_o.hideCommentsOnLoad){
				_closeComments(false)
			} else {
				_openComments(false)
			}
			if(_o.hidePostOnLoad){
				_closePost(false)
			} else {
				_openPost(false)
			}
			
			
			return $tbp
		}
		
		function _setup_events(){
			$tbp.find('.comment-toggle').bind('click',_commentToggleClickHandler)
			$tbp.find('.more-link').bind('click',_postToggleClickHandler)
		}
		
		function _commentToggleClickHandler(e){
			e.preventDefault()
			if(_commentsOpen){
				_closeComments(true)
			} else {
				_openComments(true)
			}
		}
		
		function _openComments(animate){
			_commentsOpen = true
			$tbp.find('.comment-toggle.indicator').text('Hide Comments')
			var _targetHeight = 0
			$tbp.find('.post-comments').children().each(function(i,j){
				_targetHeight += tc.jQ(j).outerHeight()
			})
			if(animate){
				$tbp.find('.post-comments').stop().animate({'opacity':1.0,'height':_targetHeight+'px'},400)
			} else {
				$tbp.find('.post-comments').stop().css('height',_targetHeight+'px').css('opacity',1.0)
			}
		}
		
		function _closeComments(animate){
			_commentsOpen = false
			$tbp.find('.comment-toggle.indicator').text('Show Comments')
			if(animate){
				$tbp.find('.post-comments').stop().animate({'opacity':0.0,'height':'0px'},400)
			} else {
				$tbp.find('.post-comments').stop().css('height','0px').css('opacity',0.0)
			}
		}
		
		function _postToggleClickHandler(e){
			e.preventDefault()
			if(_postOpen){
				_closePost(true)
			} else {
				_openPost(true)
			}
		}
		
		function _openPost(animate){
			_postOpen = true
			if(!_moreContentLoaded){
				_loadMoreContent()
				return
			}
			if(!_commentsOpen){
				_openComments(true)
			}
			$tbp.find('.more-link').html('&laquo; Read less')
			var _targetHeight = 0
			$tbp.find('.post-part2').children().each(function(i,j){
				_targetHeight += tc.jQ(j).outerHeight()
			})
			if(animate){
				$tbp.find('.post-part2').stop()
					.animate(
						{'opacity':1.0,'height':_targetHeight+'px'},
						800,
						"swing",
						function() {
							$tbp.find('.post-part2').css("height", "auto");
						}
					);
			} else {
				$tbp.find('.post-part2').stop().css('height',_targetHeight+'px')
			}
		}
		
		function _closePost(animate){
			_postOpen = false
			if(_commentsOpen){
				_closeComments(true)
			}
			$tbp.find('.more-link').html('Read more &raquo;')
			if(animate){
				$tbp.find('.post-part2').stop().animate({'opacity':0.0,'height':'0px'},800)
			} else {
				$tbp.find('.post-part2').stop().css('height','0px')
			}
		}
		
		function _loadMoreContent(){
			_showLoader(true)
			if(!_o.id){
				return false
			}
			var opts = 	{
							type:'GET',
							dataType:'html',
							success:_loadMoreContentSuccessHandler,
							error:_loadMoreContentErrorHandler,
							cache:true,
							url:'/wp-content/themes/SPUD/see_more.php?id='+_o.id
						}
			tc.jQ.ajax(opts)
		}
		
		function _loadMoreContentSuccessHandler(e,d){
			_moreContentLoaded = true
			_hideLoader(true)
			$tbp.find('.post-part2').css('opacity',0.0).html(e)
			_openPost(true)
		}
		
		function _loadMoreContentErrorHandler(){
			_hideLoader(true)
		}
		
		function _showLoader(animate){
			if(animate){
				$tbp.find('.post-part2-loader').stop().css('opacity',0.0).animate({'opacity':1.0},400)
			} else {
				$tbp.find('.post-part2-loader').stop().css('opacity',1.0)
			}
		}
		
		function _hideLoader(animate){
			if(animate){
				$tbp.find('.post-part2-loader').stop().css('opacity',1.0).animate({'opacity':0.0},400)
			} else {
				$tbp.find('.post-part2-loader').stop().css('opacity',0.0)
			}
		}
		
		return _initialize()
	}
})(tc);